/*
 * Copyright (C) 2005-2008 Alfresco Software Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

 * As a special exception to the terms and conditions of version 2.0 of 
 * the GPL, you may redistribute this Program in connection with Free/Libre 
 * and Open Source Software ("FLOSS") applications as described in Alfresco's 
 * FLOSS exception.  You should have received a copy of the text describing 
 * the FLOSS exception, and it is also available here: 
 * http://www.alfresco.com/legal/licensing"
 */

package org.alfresco.extension.avmversions.webscripts;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avm.VersionDescriptor;
import org.alfresco.web.scripts.DeclarativeWebScript;
import org.alfresco.web.scripts.Cache;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.Status;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * ####TODO: Add description
 *
 * @author Peter Monks (peter.monks@alfresco.com)
 * @version $Id$
 */
public class AVMListVersionsWebScript
    extends DeclarativeWebScript
{
    private final static Log log = LogFactory.getLog(AVMListVersionsWebScript.class);
    
    private final static String PARAMETER_NAME_AVM_STORE = "avmStore";
    
    private final static String RESPONSE_PARAMETER_AVM_STORE    = PARAMETER_NAME_AVM_STORE;
    private final static String RESPONSE_PARAMETER_VERSION_LIST = "versionList";

    private final AVMService avmService;
    
    
    public AVMListVersionsWebScript(final AVMService avmService)
    {
        // PRECONDITIONS
        assert avmService != null : "avmService must not be null.";
        
        // Body
        this.avmService = avmService;
    }


    /**
     * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptStatus, org.alfresco.web.scripts.WebScriptCache)
     */
    @Override
    protected Map<String, Object> executeImpl(final WebScriptRequest request, final Status status, final Cache cache)
    {
        Map<String, Object>       result                   = null;
        String                    avmStore                 = null;
        List<VersionDescriptor>   versionList              = null;
        List<Map<String, Object>> versionListForFreemarker = null;
        
        // PRECONDITIONS
        assert request != null : "request must not be null.";
        assert status  != null : "status must not be null.";
        assert cache   != null : "cache must not be null.";
        
        // Body
        
        // Retrieve and validate args
        avmStore          = request.getParameter(PARAMETER_NAME_AVM_STORE);
        
        if (avmStore == null || avmStore.trim().length() == 0)
        {
            throw new WebScriptException("Parameter " + PARAMETER_NAME_AVM_STORE + " is mandatory.");
        }
        
        // Retrieve list of versions for the store
        versionList = avmService.getStoreVersions(avmStore);
        
        // Build response object for FTL template
        versionListForFreemarker = new ArrayList<Map<String, Object>>(versionList.size());
        
        for (int i = 0 ; i < versionList.size(); i++)
        {
            VersionDescriptor   version    = versionList.get(i);
            Map<String, Object> properties = new HashMap<String, Object>();
            
            properties.put("tag",         version.getTag());
            properties.put("versionID",   version.getVersionID());
            properties.put("creator",     version.getCreator());
            properties.put("createDate",  new Date(version.getCreateDate()));   // This is the reason we can't pass versionList directory to Freemarker.  *ugh*
            properties.put("description", version.getDescription());
            
            versionListForFreemarker.add(i, properties);
        }
        
        result = new HashMap<String, Object>();
        result.put(RESPONSE_PARAMETER_AVM_STORE,    avmStore);
        result.put(RESPONSE_PARAMETER_VERSION_LIST, versionListForFreemarker);
        
        return(result);
    }

}
